package io.bluerain.bocket.message;

import io.bluerain.core.Obj;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.nio.charset.Charset;

/**
 * Created by foredawn on 15-8-14.
 */
public class Message {

    protected Charset charset;
    protected byte[] data;
    private MessageType type;

    public MessageType getType() {
        return type;
    }

    public void setType(MessageType type) {
        this.type = type;
    }

    public TextMessage toTextMessage() {
        TextMessage textMessage = new TextMessage(new String(data, charset));
        return textMessage;
    }

    public static Message make(byte[] data, Charset charset) {
        Message message = new Message();
        message.data = data;
        message.charset = charset;
        return message;
    }

    public DataMessage readData(InputStream inputStream, Charset charset) throws IOException {
        DataMessage message = new DataMessage(inputStream, charset);
        return message;
    }

    public String getText() {
        return toTextMessage().getText();
    }

    public enum MessageType {
        TEXT,
        BYTE
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }
}
